package com.eazy.lkv2.web.controller.traffic;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.eazy.lkv2.web.controller.BaseController;
import com.eazy.lkv2.web.entity.Area;
import com.eazy.lkv2.web.service.traffic.AreaService;

/**
 * @author jzx
 * @date 2016/2/15
 * @desc 区域管理
 */
@Controller
@RequestMapping("/area")
public class AreaController extends BaseController {

	@Autowired
	private AreaService areaService;

	/**
	 * 区域查询
	 */
	@RequestMapping(value = "list/{id}", method = RequestMethod.GET)
	public String selectArea(@PathVariable String id) {
		setAttr("city_id", id);
		List<Map<String, Object>> data = areaService.selectAreaByCityID(id);
		setAttr("data", data);
		return "traffic/area/area_list";
	}

	/**
	 * 区域添加
	 */
	@RequestMapping(value = "addArea", method = RequestMethod.POST)
	public String addArea(Area area) {
		area.setCreateTime(getSysTimestamp());
		areaService.create(area);
		return "redirect:/area/list/" + area.getCityId();
	}
	
	/**
	 * 区域修改
	 */
	@RequiresPermissions("sys:area:update")
	@RequestMapping(value = "updateArea", method = RequestMethod.POST)
	public String updateArea(Area area) {
		areaService.update(area);
		return "redirect:/area/list/" + area.getCityId();
	}

	/**
	 * 区域删除
	 */
	@RequiresPermissions("sys:area:delete")
	@RequestMapping(value = "deleteArea/{id}/{cityid}", method = RequestMethod.GET)
	public String deleteArea(@PathVariable String id, @PathVariable String cityid) {
		areaService.removeById(parseInt(id));
		return "redirect:/area/list/" + cityid;
	}

	/**
	 * 跳转到区域添加
	 */
	@RequestMapping(value = "toAddarea/{id}", method = RequestMethod.GET)
	public ModelAndView toAddarea(@PathVariable String id) {
		addObject("city_id", id);
		return getSuccess("traffic/area/area_add");
	}

	/**
	 * 跳转到区域修改
	 */
	@RequestMapping(value = "toUpdatearea/{id}", method = RequestMethod.GET)
	public ModelAndView toUpdatearea(@PathVariable String id) {
		addObject("data", areaService.findById(parseInt(id)));
		return getSuccess("traffic/area/area_update");
	}
}
